package com.yaozheng.vocationaltraining.service.impl.userinfo;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.userinfo.ILogoutView;
import com.yaozheng.vocationaltraining.service.userinfo.LogoutService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import java.util.HashMap;

/**
 * 退出
 */
@EBean
public class LogoutServiceImpl implements LogoutService {
    ILogoutView iLogoutView;

    @Override
    public void init(ILogoutView iLogoutView) {
        this.iLogoutView = iLogoutView;
    }

    @Background
    @Override
    public void logout() {
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("aaa", "bbb");
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/user/logout",
                    iLogoutView.getToken(), para, new BaseView(iLogoutView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iLogoutView.logoutSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iLogoutView.logoutError(error);
                        }

                    });
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iLogoutView.isResponseResult()) {
                iLogoutView
                        .logoutError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
